package org.jeecg.modules.lab.items.record.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.lab.entrust.entity.LabEntrustItems;
import org.jeecg.modules.lab.entrust.entity.LabItemsRecordNorm;
import org.jeecg.modules.lab.entrust.entity.LabItemsRecordSample;
import org.jeecg.modules.lab.entrust.entity.LabItemsRecordTarget;
import org.jeecg.modules.lab.items.temp.entity.LabItemsTempNorm;
import org.jeecg.modules.lab.items.record.mapper.LabItemsRecordNormMapper;
import org.jeecg.modules.lab.items.record.service.ILabItemsRecordNormService;
import org.jeecg.modules.lab.items.temp.mapper.LabItemsTempNormMapper;
import org.jeecg.modules.lab.items.temp2.entity.LabItemsTemp2Basic;
import org.jeecg.modules.lab.items.temp2.entity.LabItemsTemp2Norm;
import org.jeecg.modules.lab.items.temp2.mapper.LabItemsTemp2BasicMapper;
import org.jeecg.modules.lab.items.temp2.mapper.LabItemsTemp2NormMapper;
import org.jeecg.modules.lab.items.temp2.service.ILabItemsTemp2NormService;
import org.jeecg.modules.lab.items.vo.LabRecordNorm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @Description: 项目检测记录_检测条件信息
 * @Author: jeecg-boot
 * @Date: 2023-10-08
 * @Version: V1.0
 */
@Service
public class LabItemsRecordNormServiceImpl extends ServiceImpl<LabItemsRecordNormMapper, LabItemsRecordNorm> implements ILabItemsRecordNormService {

    @Autowired
    private LabItemsTempNormMapper labItemsTempNormMapper;

    @Autowired
    private LabItemsRecordNormMapper labItemsRecordNormMapper;

    @Autowired
    private LabItemsTemp2NormMapper labItemsTemp2NormMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertNormData(LabEntrustItems labEntrustItems) {
        String task_id = labEntrustItems.getEntrustId();
        String task_item_id = labEntrustItems.getId();
        String item_id = labEntrustItems.getItemId();
        QueryWrapper<LabItemsTempNorm> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("item_id", item_id)
                .orderByAsc("field_sort");
        List<LabItemsTempNorm> labItemsTempNormList = labItemsTempNormMapper.selectList(queryWrapper);
        for (LabItemsTempNorm labItemsTempNorm : labItemsTempNormList) {
            // TODO: 处理每个实例 labItemsTempNorm
            LabItemsRecordNorm labItemsRecordNorm = new LabItemsRecordNorm();
            labItemsRecordNorm.setTaskId(task_id);
            labItemsRecordNorm.setTaskItemId(task_item_id);
            labItemsRecordNorm.setItemId(item_id);
            labItemsRecordNorm.setFieldName(labItemsTempNorm.getFieldName());
            labItemsRecordNorm.setFieldCode(labItemsTempNorm.getFieldCode());
            labItemsRecordNorm.setFieldValue(labItemsTempNorm.getFieldValue());
            labItemsRecordNorm.setFieldInput(labItemsTempNorm.getFieldInput());
            labItemsRecordNorm.setFieldOption(labItemsTempNorm.getFieldOption());
            labItemsRecordNorm.setFieldSort(labItemsTempNorm.getFieldSort());
            labItemsRecordNorm.setFieldType(labItemsTempNorm.getFieldType());
            labItemsRecordNorm.setFieldRow(labItemsTempNorm.getFieldRow());
            labItemsRecordNorm.setFieldCol(labItemsTempNorm.getFieldCol());
            labItemsRecordNorm.setFieldRowspan(labItemsTempNorm.getFieldRowspan());
            labItemsRecordNorm.setFieldColspan(labItemsTempNorm.getFieldColspan());
            labItemsRecordNorm.setSynFlag("0");
            labItemsRecordNormMapper.insert(labItemsRecordNorm);
        }
    }

    @Override
    public List<LabRecordNorm> handleNormData(String entrustId, String taskItemId, String itemId) {
        List<LabRecordNorm> recordNormList = labItemsRecordNormMapper.getRecordNormList(entrustId, itemId);
        QueryWrapper<LabItemsRecordNorm> queryWrapper = new QueryWrapper<>();
        for (LabRecordNorm labRecordNorm : recordNormList) {
            queryWrapper.clear();
            if (!"".equals(taskItemId) && taskItemId != null) {
                queryWrapper.eq("task_item_id", taskItemId);
            }
            queryWrapper.eq("field_row", labRecordNorm.getNId())
                    .eq("task_id", entrustId)
                    .eq("item_id", itemId)
                    .orderByAsc("field_sort");
            List<LabItemsRecordNorm> itemsRecordNormList = labItemsRecordNormMapper.selectList(queryWrapper);
            labRecordNorm.setItemsRecordNormList(itemsRecordNormList);
        }
        return recordNormList;
    }

    @Override
    public List<LabItemsRecordNorm> handleNormDataApp(String entrustId, String taskItemId, String itemId) {
        QueryWrapper<LabItemsRecordNorm> queryWrapper = new QueryWrapper<>();
        queryWrapper.clear();
        queryWrapper.eq("task_id", entrustId)
                .eq("item_id", itemId)
                .eq("task_item_id",taskItemId)
                .ne("field_input", "0")
                .orderByAsc("field_sort");
        List<LabItemsRecordNorm> itemsRecordNormList = labItemsRecordNormMapper.selectList(queryWrapper);
        return itemsRecordNormList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(String entrustId, String itemId, List<LabRecordNorm> recordNormList) {
        //1、先根据委托/监督Id、项目id条件删除数据
        QueryWrapper<LabItemsRecordNorm> queryWrapper = new QueryWrapper<>();
        queryWrapper.clear();
        queryWrapper.eq("task_id", entrustId)
                .eq("item_id", itemId);
        labItemsRecordNormMapper.delete(queryWrapper);
        //2、重新插入数据
        if (recordNormList != null && recordNormList.size() > 0) {
            for (LabRecordNorm recordNorms : recordNormList) {
                for (LabItemsRecordNorm norm : recordNorms.getItemsRecordNormList()) {
                    labItemsRecordNormMapper.insert(norm);
                }
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdateApp(String entrustId, String itemId, List<LabItemsRecordNorm> itemsRecordNormList) {
        //1、重新检测条件数据
        if (itemsRecordNormList != null && itemsRecordNormList.size() > 0) {
            for (LabItemsRecordNorm norm : itemsRecordNormList) {
                labItemsRecordNormMapper.updateById(norm);
            }
        }
    }

    /* 根据 委托id和项目id*/
    @Override
    public List<LabItemsRecordNorm> byEntrustItemId(String entrustId, String itemId) {
        QueryWrapper<LabItemsRecordNorm> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("task_id", entrustId)
                .eq("item_id", itemId)
                .eq("field_input", "1");
        return labItemsRecordNormMapper.selectList(objectQueryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void kyInsertNormData(LabEntrustItems labEntrustItems) {
        String task_id = labEntrustItems.getEntrustId();
        String item_id = labEntrustItems.getItemId();
        String task_item_id = labEntrustItems.getId();

        //先删除历史数据
        QueryWrapper<LabItemsRecordNorm> wrapper = new QueryWrapper<>();
        wrapper.eq("item_id", item_id);
        labItemsRecordNormMapper.delete(wrapper);

        QueryWrapper<LabItemsTemp2Norm> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("items_id", item_id).orderByAsc("field_sort");
        List<LabItemsTemp2Norm> labItemsTemp2NormList = labItemsTemp2NormMapper.selectList(queryWrapper);
        //循环生成Norm数据
        if (labItemsTemp2NormList != null) {
            for (int i = 0; i < labItemsTemp2NormList.size(); i++) {

                LabItemsTemp2Norm norm = labItemsTemp2NormList.get(i);
                String field_name = norm.getFieldName();
                //插入名称列
                LabItemsRecordNorm norma = new LabItemsRecordNorm();
                norma.setTaskId(task_id);
                norma.setItemId(item_id);
                norma.setTaskItemId(task_item_id);
                norma.setFieldName(field_name);
                norma.setFieldInput("0");
                norma.setFieldSort(i + 1);
                norma.setFieldType("String");
                norma.setFieldRow(i + 1);
                norma.setFieldCol(1);
                norma.setFieldRowspan(0);
                norma.setFieldColspan(0);
                norma.setSynFlag("0");
                labItemsRecordNormMapper.insert(norma);
                //插入输入列
                LabItemsRecordNorm normb = new LabItemsRecordNorm();
                normb.setTaskId(task_id);
                normb.setItemId(item_id);
                normb.setTaskItemId(task_item_id);
                normb.setFieldName(field_name);
                normb.setFieldCode("norm_value" + (i + 1));
                normb.setFieldInput("1");
                normb.setFieldSort(i + 2);
                normb.setFieldType("String");
                normb.setFieldRow(i + 1);
                normb.setFieldCol(2);
                normb.setFieldRowspan(0);
                normb.setFieldColspan(0);
                normb.setSynFlag("0");
                labItemsRecordNormMapper.insert(normb);
            }
        }
    }

    @Override
    public void deleteByTaskId(String taskId) {
        labItemsRecordNormMapper.deleteByTaskId(taskId);
    }
}
